package com.central.apps.handler.contract;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.central.app.service.HandlerService;
import com.central.apps.handler.base.LvBaseHandler;
import com.central.bizcommon.model.flow.FlowNode;
import com.central.bizcommon.model.flow.FlowOpinion;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class ConpoHandler extends LvBaseHandler {
    @Autowired
    private HandlerService handlerService;
    @Override
    public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
        //将主表中的评审意见添加到对应的子表评审意见位置。
      if(!isUpdate&&"gq_po".equals(objName)){
          int conid= MapUtils.getIntValue(data,"conid");
          int conpid = MapUtils.getIntValue(data,"conpid");//合同申请主键id
          int cconid= MapUtils.getIntValue(data,"cconid");//框架合同序号
          int cconpid = MapUtils.getIntValue(data,"cconpid");//框架合同主键id
          //查询合同申请中的物料明细
          List<Map<String,Object>> list = handlerService.findBySql("select * from gq_conpoline where conid ="+conid+" and conpid="+conpid);
          if(CollectionUtils.isNotEmpty(list)){
              for (Map<String, Object> stringObjectMap : list) {
                  stringObjectMap.put("ponum",data.get("ponum"));
                  stringObjectMap.put("poid",data.get("id"));
                  stringObjectMap.put("cconid",cconid);
                  stringObjectMap.put("cconpid",cconpid);
                  handlerService.insert("gq_poline",stringObjectMap);
              }
          }
      }



    }
    @Override
    public void beforSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
        if(isUpdate&&"gq_po".equals(objName)){

            int id = MapUtils.getIntValue(data,"id");
            List<Map<String,Object>> list = handlerService.findBySql("select applicationnum,conid,conpid from gq_po where id ="+id);
            String applicationnum = MapUtils.getString(data,"applicationnum");
            String applicationnum1 = MapUtils.getString(list.get(0),"applicationnum");
            if(!applicationnum.equals(applicationnum1)){
                //先删除，再添加 插入到外协订单中的物料明细
                int conid= MapUtils.getIntValue(list.get(0),"conid");
                int conpid = MapUtils.getIntValue(list.get(0),"conpid");//合同申请主键id
                handlerService.updateBySql("delete from gq_poline where conid ="+conid+" and conpid="+conpid);//删除旧数据
                //查询合同申请中的物料明细
                int conid1= MapUtils.getIntValue(data,"conid");
                int conpid1 = MapUtils.getIntValue(data,"conpid");//合同申请主键id
                int cconid= MapUtils.getIntValue(data,"cconid");//框架合同序号
                int cconpid = MapUtils.getIntValue(data,"cconpid");//框架合同主键id
                List<Map<String,Object>> list2 = handlerService.findBySql("select * from gq_conpoline where conid ="+conid1+" and conpid="+conpid1);//插入新数据

                if(CollectionUtils.isNotEmpty(list2)){
                    for (Map<String, Object> stringObjectMap : list2) {
                        stringObjectMap.put("ponum",data.get("ponum"));
                        stringObjectMap.put("poid",data.get("id"));
                        stringObjectMap.put("cconid",cconid);
                        stringObjectMap.put("cconpid",cconpid);
                        handlerService.insert("gq_poline",stringObjectMap);
                    }
                }
            }
        }
    }
    @Override
    public String beforeExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op)throws Exception {
        if ("submit2".equals(flowNode.getId()) && op.isFlag()) {
            List<Map<String, Object>> list = handlerService.findBySql("select applicationnum,conid,conpid,ponum from gq_po where id =" + id);
            for (Map<String, Object> stringObjectMap : list) {
                List<Map<String, Object>> list2 = handlerService.findBySql("select * from gq_poline where conid =" + stringObjectMap.get("conid") + " and conpid =" + stringObjectMap.get("conpid") + " and" +
                        " ponum='" + stringObjectMap.get("ponum") + "' and poid =" + id);

                if (CollectionUtils.isNotEmpty(list2)) {
                    for (Map<String, Object> stringObjectMap2 : list2) {
                        stringObjectMap2.put("qualquantity", stringObjectMap2.get("unreceivedqty"));
                        System.out.println("stringObjectMap2.get(\"iscost\")==========="+stringObjectMap2.get("iscost"));
                        if(!"是".equals(stringObjectMap2.get("iscost"))){
                            BigInteger poslid = (BigInteger) handlerService.insert("gq_posline", stringObjectMap2);
                            stringObjectMap2.put("poslid",poslid);
                        }
                        stringObjectMap2.put("arrquantity", stringObjectMap2.get("unreceivedqty"));
                        //stringObjectMap2.put("linecost", 10);//行成本，暂时设置为10
                        //stringObjectMap2.put("acceptments", "01");//检查字段

                        handlerService.update("gq_poline",stringObjectMap2);


                    }
                }
            }
        }

        return super.beforeExcuteFlow(app, id, flowNode, op);
    }

}
